//
//  MTFeedBackViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/24.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTFeedBackViewController.h"
#import "LCPlaceholderTextView.h"
#import <AFHTTPSessionManager.h>
#import <MBProgressHUD.h>
#import "MTImagePickViewController.h"
#import "MTImageCollectionViewCell.h"

@interface MTFeedBackViewController ()<UITextViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) LCPlaceholderTextView *textView;
@property (strong, nonatomic) UICollectionView *myImagesCollectionView;
@property (nonatomic, strong) UIButton *btn;

@end

@implementation MTFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigation];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupTextView];
    [self setupUI];

    
}
- (void)setupTextView{
    
    LCPlaceholderTextView *textView = [[LCPlaceholderTextView alloc] init];
    textView.frame = CGRectMake(10, 0, MTScreenW - 20, 102);
    //设置可以上下拖动
    textView.alwaysBounceVertical = YES;
    textView.delegate =self;
    textView.placeholder = @"请输入你想反馈的问题...";
    [self.view addSubview:textView];
    self.textView = textView;

}

-(void)setupUI {
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(10, 102, 60, 60);
//    [btn setImage:[UIImage imageNamed:@"Btn_Add-to"] forState:UIControlStateNormal];
//    [btn setImage:[UIImage imageNamed:@"Btn_Add-to_pre"] forState:UIControlStateHighlighted];
//    [btn addTarget:self action:@selector(chosePhoto) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
//    self.btn = btn;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.myImagesCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 102, MTScreenW-2*15, 80) collectionViewLayout:layout];
    self.myImagesCollectionView.scrollEnabled = NO;
    [self.myImagesCollectionView setBackgroundView:nil];
    [self.myImagesCollectionView setBackgroundColor:[UIColor clearColor]];
    [self.myImagesCollectionView registerClass:[MTImageCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MTImageCollectionViewCell class])];
    self.myImagesCollectionView.dataSource = self;
    self.myImagesCollectionView.delegate = self;
    [self.view addSubview:self.myImagesCollectionView];

    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(10, 202, MTScreenW - 20, 1);
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(20, 257, MTScreenW - 40, 43);
    [btn1 setBackgroundImage:[UIImage imageNamed:@"BtN_Submit"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"BtN_Submit_pre"] forState:UIControlStateHighlighted];
    [btn1 addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    [self.view addSubview:btn1];
    [btn1 setTitle:@"提交" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}

#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    
}

-(void)chosePhoto {

//    //从系统相册选择一张图片
//    //弹出系统相册
//    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
//    //设置照片来源
//    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//    //modal
//    [self presentViewController:pickVC animated:YES completion:nil];
//
//    pickVC.delegate = self;

    MTImagePickViewController *imagepickVc = [[MTImagePickViewController alloc] init];
    [self.navigationController presentViewController:imagepickVc animated:YES completion:nil];
}

//#pragma mark -UIImagePickerControllerDelegate
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//
//    [self dismissViewControllerAnimated:YES completion:nil];
//
//    UIImage *image = info[UIImagePickerControllerOriginalImage];
//
//    //把图片转成二进制/Users/loujianwei/Desktop/.DS_Store
//    NSData *data =  UIImagePNGRepresentation(image);
//
////    [data writeToFile:@"/Users/loujianwei/Desktop/photo.png" atomically:YES];
//
//    [self.btn setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
//
//
//}

-(void)submit {
    NSLog(@"提交服务器");

    AFHTTPSessionManager *msg = [AFHTTPSessionManager manager];

    /*
     第一个参数:请求路径(NSString)
     第二个参数:非文件参数(NSDictionary)
     第三个参数:constructingBodyWithBlock 拼接要上传的文件参数的
     第四个参数:progress 进度回调
     第五个参数:success 成功之后的回调
     responseObject:响应体信息
     第六个参数:failure 失败之后的回调
     */
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    [msg POST:@"" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/loujianwei/Desktop/.DS_Store"] name:@"file" error:nil];

    } progress:^(NSProgress * _Nonnull uploadProgress) {

        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"%@-上传成功",responseObject);
        [self mbHUD:@"正在上传中"];
        //返回主界面
        [self backClick];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        [self mbHUD:@"上传失败,请从新上传"];
         NSLog(@"%@--上传失败",error);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    self.textView.isEditable = NO;
     [self.textView resignFirstResponder];
//    [self.textView endEditing:YES];
}

#pragma mark -- UITextFieldDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //拖拽时 结束编辑 退出键盘
    
}

#pragma mark - MB指示器HUD
- (void)mbHUD:(NSString *)title {

    MBProgressHUD * mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    mb.labelText = title;
    mb.removeFromSuperViewOnHide = YES;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [mb hide:YES];
    });
}

-(void)initNavigation {
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"back"] highImage:[UIImage imageNamed:@"back anxia"] target:self action:@selector(backClick)];
    self.navigationItem.title = @"反馈与帮助";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0x45c75a)]  forBarMetrics:UIBarMetricsDefault];
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
    attr[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
}

-(void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
